<?php
/* 
* [文章与分类关联视图模型]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-19 17:34:57
* @Last Modified time: 2014-06-26 17:13:39
*/
class ArticleCateViewModel extends ViewModel{
	//定义主表
	public $table = 'article';

	//定义关联表信息
	public $view = array(
		//定义category表规则
		'category' => array( 
			//指定连接方式
			'type' => INNER_JOIN, 
			//关联条件
			'on' => 'article.category_cid=category.cid'
		),
		'article_data'=>array(
			//指定连接方式
			'type' => INNER_JOIN, 
			//关联条件
			'on' => 'article.aid=article_data.article_aid'
			)
	);


	/**
	 * [get_all 获得关联两张表的数据]
	 * @param  [type] $where [description]
	 * @param  [type] $field [description]
	 * @return [type]        [description]
	 */
	public function get_all($where=NULL,$field=NULL,$rows=10,$pageNum=3,$pageStyle=3){
		//分页类使用
		//参数1.总数，2.每页多少条，3.显示几个页码,4.页码的风格
		$total = $this->where($where)->count();
		//实例化分页类
		$page = new Page($total,$rows,$pageNum,$pageStyle);
		//分页显示
		$pageData = $page->show();
		//别忘记用limit
		$data = $this->where($where)->field($field)->all($page->limit());
		$data['page'] = $pageData;

		return $data;
	}


	public function get_one($aid){
		return $this->where(array('aid'=>$aid))->find();
	}


	public function get_join_data($table,$field=NULL,$limit=NULL){
		return $this->join($table)->field($field)->all($limit);
	}

	/**
	 * [get_join_count description]
	 * @param  [type] $table [description]
	 * @return [type]        [description]
	 */
	public function get_join_count($table){
		return $this->join($table)->count();
	}
}
